<?php

namespace common\model\sell66;

use common\bot\BotListService;
use common\db\Sell66;
use waylaidwanderer\SteamCommunity\SteamCommunity;

class Bot extends Sell66 {
	// 设置当前模型对应的完整数据表名称
	protected $table = 'bots';
	protected $botId = '';
    protected $steam = null;

    public static $clientNoMaps = [
        'F',
    ];

    public static $activeMaps = [
        'active' => 1,
        'default' => 0,
    ];
    public static $loginStatusMaps = [
    	'login'=>1,
    	'default'=>0,
    ];
    
    public static $stopMaps = [
    	'stop'=>1,
    	'active'=>0
    ];

    public static $fromMaps = [
        'can'=>1,
        'cant'=>2
    ];

    public function getSteam() {
		return $this->steam;
	}
    public function setSteam(SteamCommunity $steam) {
		$this->steam = $steam;
	}
	
	public static  function getOneLoginRobot(){
		return static::where(
				['login_status'=>Bot::$loginStatusMaps['login'],
				'is_active'=>self::$activeMaps['active'],
				'is_stop'=>self::$stopMaps['active'],
                'client_no' => ['in', self::$clientNoMaps],
				])->order('rand()')->find();
	}

	/**
	 * 获取所有收货机器人
	 * @return array
	 */
	public static function getAllReceiptRobot($robotId = 0)
	{
		$where = array(
			'login_status' => self::$loginStatusMaps['login'],
			'is_active' => self::$activeMaps['active'],
			'is_stop' => self::$stopMaps['active'],
		);
		if ($robotId != 0) {
			$where['id'] = $robotId;
		}
		return static::where($where)->select();
	}
	
	public static function saveBotLogin($botId,$login = true){
		
		 static::where(['id'=>$botId])
				->update(['login_status'=> $login ? Bot::$loginStatusMaps['login'] :Bot::$loginStatusMaps['default'],
						'last_login_time'=>date('Y-m-d H:i:s')
					]);
		 if($login !== true){

		 }
		 return true;
	}
	
	public static function getOneBotSettings($botId){
		$bot = static::where(['id'=>$botId])->find();
		if(empty($bot)){
			return false;
		}
		return  [
			'id'		=> $bot['id'],
			'botId' 	=> $bot['steam_account'],
			'username'  => $bot['steam_account'],
			'password'  => $bot['password'],
			'apiKey' 	=> $bot['apikey'],
			'mobileAuth'=> [
			"sharedSecret" => $bot['shared_secret'],
			"identitySecret" => $bot['identity_secret'],
			"deviceId" => "android:" . preg_replace('/^([0-9a-f]{8})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{12}).*$/', '$1-$2-$3-$4-$5', md5($bot['steam_id']))
			]
		];
	}
	public static function getAllBotSettings($isActive = false){
	    $second = 1;
		if($isActive === true){
			$bots = static::where(['is_active'=>self::$activeMaps['active'], 'bot_second'=>$second])->select();
		}elseif($isActive === false){
			$bots = static::all();
		}else{
			return [];
		}
		$configs = array();
		foreach ($bots as $bot){
			$configs[] = [
					'id'		=> $bot['id'],
					'botId' 	=> $bot['steam_account'],
					'username'  => $bot['steam_account'],
					'password'  => $bot['password'],
					'apiKey' 	=> $bot['apikey'],
					'mobileAuth'=> [
						"sharedSecret" => $bot['shared_secret'],
						"identitySecret" => $bot['identity_secret'],
						"deviceId" => "android:" . preg_replace('/^([0-9a-f]{8})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{12}).*$/', '$1-$2-$3-$4-$5', md5($bot['steam_id']))
					]
			];
		}
		return $configs;
	}
	public function getBotSettings($botId=1) {
		$config = [
			'1'=>[
				'botId' => getenv('bot1_id'),
				'username' => getenv('bot1_username'),
				'password' => getenv('bot1_password'),
				'apiKey' => getenv('bot1_apiKey'),
				'mobileAuth' => [
						"sharedSecret" => getenv('bot1_sharedSecret'),
						"identitySecret" => getenv('bot1_identitySecret'),
						"deviceId" => "android:" . preg_replace('/^([0-9a-f]{8})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{12}).*$/', '$1-$2-$3-$4-$5', md5(getenv('bot1_steamid')))
						]
				],
			'2'=>[ 
				'botId' => getenv('bot2_id'),
				'username' => getenv('bot2_username'),
				'password' => getenv('bot2_password'),
				'apiKey' => getenv('bot2_apiKey'),
				'mobileAuth' => [ 
						"sharedSecret" => getenv('bot2_sharedSecret'),
						"identitySecret" => getenv('bot2_identitySecret'),
						"deviceId" => "android:" . preg_replace('/^([0-9a-f]{8})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{12}).*$/', '$1-$2-$3-$4-$5', md5(getenv('bot2_steamid')))
				] 
			],
		]; 
		$settings = $config[$botId];
		$this->botId = $settings ['botId'];
		return $settings;
	}
	public function getBotId() {
		return $this->botId;
	}

	/**
	 * 获取机器人信息
	 * @param  int $botId 机器人ID
	 * @return object
	 */
	public static function getBotInfoById($botId)
	{
		$where = array(
			'id' => $botId,
			'login_status' => self::$loginStatusMaps['login'],
			'is_active' => self::$activeMaps['active'],
		);

		return static::where($where)->find();
	}

    /**
     * 获取检测交易链接的机器人
     * @return bool
     */
    public static function getCheckTradeUrlLivingBot()
    {
        //获取当前机器的机器人
        $condition['client_no'] = getenv('APP_NO');
        if(empty($condition['client_no'])){
            return false;
        }

        $condition['login_status'] = static::$loginStatusMaps['login'];
        $condition['is_active'] = static::$activeMaps['active'];
        $condition['is_stop'] = static::$stopMaps['active'];
        $canFromBotList = static::where($condition)->order('last_login_time desc')->limit(10)->select();
        if (empty($canFromBotList)) {
            return false;
        }

        shuffle($canFromBotList);
        $rand = array_rand($canFromBotList);
        $botLiving = $canFromBotList[$rand];
        $botLiving['steam'] = BotListService::getInstance()->getBot($botLiving['steam_account']);
        return $botLiving;
    }

	/**
	 * 获取机器人信息
	 * @param  int $botId 机器人ID
	 * @return object
	 */
    public static function getRobotByBotId($botId = '')
    {
		$where = array(
			'client_no' => getenv('APP_NO'),
		);
		if ($botId) {
			$where['id'] = $botId;
		}

		$result = static::field('id, steam_id, steam_account, apikey')->where($where)->select();

		return !empty($result) ? collection($result)->toArray() : array();
    }

    public static function getOneRobotByBotId($botId = '')
    {
		$where = array(
			'client_no' => getenv('APP_NO'),
			'is_stop' => self::$stopMaps['active'],
		);
		if ($botId) {
			$where['id'] = $botId;
		}

		$result = static::field('id, client_no, steam_id, steam_account, apikey')->where($where)->order('last_login_time desc')->find();

		return !empty($result) ? $result->toArray() : array();
    }
}